package com.example.choupaiapp.main.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.adapter.BaseAdapterHelper;
import com.example.choupaiapp.common.adapter.QuickAdapter;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.common.view.XGridView;
import com.example.choupaiapp.main.bean.InterestLabelBean;
import com.example.choupaiapp.main.bean.SendCodeBean;
import com.example.choupaiapp.main.bean.UserInterestBean;
import com.example.choupaiapp.main.constant.Url;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;

import butterknife.BindView;

/**
 * Created by ASUS on 2019/10/31.
 */

public class InterestLabelActivity extends CommonActivity {

    @BindView(R.id.listview)
    ListView listview;
    private int count = 0;
    private QuickAdapter<InterestLabelBean.ListBeanX> quickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest_label);
    }

    @Override
    protected void initView() {
        setTitle("兴趣");

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                showDialog(InterestLabelActivity.this, "保存中...");
                JSONArray jsonArray = new JSONArray();
                count = 0;
                for (int i = 0; i < quickAdapter.getCount(); i++) {
                    for (int j = 0; j < quickAdapter.getItem(i).getList().size(); j++) {
                        if (quickAdapter.getItem(i).getList().get(j).isLabel()) {
                            jsonArray.put(quickAdapter.getItem(i).getList().get(j).getId());
                            count = count + 1;
                        }
                    }
                }
                if (count < 3) {
                    dismissDialog();
                    showImageToas("最多选择10项，最少选择3项");
                } else {
                    if (count < 11) {
                        finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.UPDATE_USER_INTEREST, null, jsonArray.toString(), new Callback<SendCodeBean>() {
                            @Override
                            public void onSuccess(SendCodeBean sendCodeBean) {
                                dismissDialog();
                                if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                    InterestLabelActivity.super.onBackPressed();
                                } else {
                                    if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                    }else{
                                        showImageToas(sendCodeBean.getMsg());
                                    }
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                            }
                        });
                    } else {
                        dismissDialog();
                        showImageToas("最多选择10项，最少选择3项");
                    }
                }
            }
        });

        quickAdapter = new QuickAdapter<InterestLabelBean.ListBeanX>(context, R.layout.item_interest_label) {
            @Override
            protected void convert(BaseAdapterHelper helper, InterestLabelBean.ListBeanX item1) {
                helper.setText(R.id.textview, item1.getName());

                XGridView gridView = helper.getView(R.id.gridview);

                QuickAdapter<InterestLabelBean.ListBeanX.ListBean> quickAdapter_child =
                        new QuickAdapter<InterestLabelBean.ListBeanX.ListBean>(context, R.layout.item_interest_label_child, item1.getList()) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, InterestLabelBean.ListBeanX.ListBean item) {
                                TextView label = helper.getView(R.id.label);
                                label.setText(item.getName());
                                if (item.isLabel()) {
                                    label.setSelected(true);
                                } else {
                                    label.setSelected(false);
                                }

                                label.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (Utils.isDoubleClick()) {
                                            return;
                                        }

                                        if (item.isLabel()) {
                                            item.setLabel(false);
                                        } else {
                                            item.setLabel(true);
                                        }
                                        notifyDataSetChanged();

                                    }
                                });
                            }
                        };

                gridView.setAdapter(quickAdapter_child);
            }
        };

        listview.setAdapter(quickAdapter);

    }

    @Override
    protected void initData() {
        showDialog(context, null);
        finalOkGo.requestHead(RequestType.POSTHEAD, Url.INTEREST_LIST, null, Url.INTEREST_LIST, new Callback<InterestLabelBean>() {
            @Override
            public void onSuccess(InterestLabelBean interestLabelBean) {
                if (interestLabelBean.getCode() == Contacts.SUCCESS) {
                    finalOkGo.requestHead(RequestType.POSTHEAD, Url.USER_INTEREST_LIST, null, Url.USER_INTEREST_LIST, new Callback<UserInterestBean>() {
                        @Override
                        public void onSuccess(UserInterestBean userInterestBean) {
                            if (userInterestBean.getCode() == Contacts.SUCCESS) {
                                for (int i = 0; i < interestLabelBean.getList().size(); i++) {
                                    for (int j = 0; j < interestLabelBean.getList().get(i).getList().size(); j++) {
                                        for (int k = 0; k < userInterestBean.getList().size(); k++) {
                                            if (interestLabelBean.getList().get(i).getList().get(j).getId() == userInterestBean.getList().get(k).getId()) {
                                                interestLabelBean.getList().get(i).getList().get(j).setLabel(true);
                                            }
                                        }
                                    }
                                }
                                quickAdapter.clear();
                                quickAdapter.addAll(interestLabelBean.getList());
                                quickAdapter.notifyDataSetChanged();
                                dismissDialog();
                            } else {
                                dismissDialog();
                                if (userInterestBean.getCode() == Contacts.TOKEN_EMPTY) {
                                }else{
                                    showImageToas(userInterestBean.getMsg());
                                }
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            dismissDialog();
                        }
                    });
                } else {
                    dismissDialog();
                    if (interestLabelBean.getCode() == Contacts.TOKEN_EMPTY) {
                    }else{
                        showImageToas(interestLabelBean.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

}
